package com.example.servlet.manager;

import com.example.service.ManagerService;
import com.example.service.impl.ManagerServiceImpl;
import com.example.utils.MybatisUtil;
import com.example.utils.ThymeleafUtil;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.thymeleaf.context.Context;

import java.io.IOException;

@WebServlet("/manager_login")
public class mLoginServlet extends HttpServlet {
    private ManagerService service;
    private SqlSession sqlSession;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        sqlSession = MybatisUtil.getSession();
        service = new ManagerServiceImpl(sqlSession);
    }

    @Override
    public void destroy() {
        sqlSession.close();
        super.destroy();
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            String m_account = null;
            String m_password = null;
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("m_account")) m_account = cookie.getValue();
                if (cookie.getName().equals("m_password")) m_password = cookie.getValue();
            }
            if (m_account != null && m_password != null) {
                // 登录校验
                if (service.auth(m_account, m_password, req.getSession())) {
                    resp.sendRedirect("manager_index1");
                    return;
                }
            }
        }
        Context context = new Context();
        if (req.getSession().getAttribute("manager_login_failure") != null) {
            context.setVariable("manager_login_failure", true);
            req.getSession().removeAttribute("manager_login_failure");
        }
        if (req.getSession().getAttribute("manager") != null) {
            resp.sendRedirect("manager_index1");
            return;
        }
        ThymeleafUtil.process("manager_login.html", context, resp.getWriter());
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String m_account = req.getParameter("m_account");
        String m_password = req.getParameter("m_password");
        String remember = req.getParameter("remember-me");
        if (service.auth(m_account, m_password, req.getSession())) {
            if (remember != null) {
                Cookie cookie_m_account = new Cookie("m_account", m_account);
                cookie_m_account.setMaxAge(60 * 60 * 24);
                Cookie cookie_m_password = new Cookie("m_password", m_password);
                cookie_m_password.setMaxAge(60 * 60 * 24);
                resp.addCookie(cookie_m_account);
                resp.addCookie(cookie_m_password);
            }
            resp.sendRedirect("manager_index1");
        } else {
            req.getSession().setAttribute("manager_login_failure", new Object());
            this.doGet(req, resp);
        }
    }
}
